﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Data.SqlTypes;
using System.Transactions;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.BinLocationCommand;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.StorageCommandHandlder
{
    public class StorageCreateCommandHandlder : IRequestHandler<StorageCreateCommand, ApiResult<int>>
    {
        private readonly ILogger<StorageCreateCommandHandlder> _logger;
        private readonly IStorageRepository _storageRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        public StorageCreateCommandHandlder(ILogger<StorageCreateCommandHandlder> logger, IStorageRepository storageRepository, IStorageProductsRepository storageProductsRepository)
        {
            _logger = logger;
            _storageRepository = storageRepository;
            _storageProductsRepository = storageProductsRepository;
        }
        public async Task<ApiResult<int>> Handle(StorageCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            _logger.LogInformation("添加入库成功");
            //System.Data.IsolationLevel.Serializable
            using (var tran = _storageRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                        //入库表添加一条数据
                        var now = DateTime.Now;
                        Storage storage = new Storage()
                        {
                            StorageNo = request.StorageNo,
                            StorageType = request.StorageType,
                            AssociateNumber = request.AssociateNumber,
                            VendorNo = request.VendorNo,
                            VendorName = request.VendorName,
                            VendorContact = request.VendorContact,
                            VendorPhone = request.VendorPhone,
                            SingleMaker = request.SingleMaker,
                            SingleTime = request.SingleTime,
                            StorageRemarks = request.StorageRemarks,
                            StorageStatus = AuditStatus.等待审核,
                            ExamineReason = request.ExamineReason,
                            ExaminePeople = request.ExaminePeople,
                            ExamineTime = request.ExamineTime,
                            CreateTime = now,
                            office_id = request.office_id,
                            user_id = request.user_id,
                            IsDelete = false,
                        };
                         await _storageRepository.AddAsync(storage);
                        //给入库产品信息添加
                        foreach (var item in request.StorageProducts)
                        {
                        StorageProducts storageProducts = new StorageProducts()
                        {
                            StorageId = storage.StorageId,
                            ProductNo = item.ProductNo,
                            ProductName = item.ProductName,
                            ProductSpec = item.ProductSpec,
                            Batch = item.Batch,
                            Price = item.Price,
                            StorageNum = item.StorageNum,
                            TotalPrice = item.TotalPrice,
                            BinLocationName = item.BinLocationName,
                            office_id = item.office_id,
                            user_id = item.user_id,
                            CreateTime = now,
                            IsDelete = false,
                        };
                          await _storageProductsRepository.AddAsync(storageProducts);
                        }
                        var list = await _storageRepository.DB.SaveChangesAsync();
                        tran.Commit();
                        result.Code = list;
                }
                catch (Exception el)
                {
                    result.Code = -1;
                    result.Message = el.Message;
                    _logger.LogError(el, el.Message);
                    tran.Rollback();
                }
            }
            return result;
        }
    }
}
